<template>
  <el-drawer ref="drawer" :title="settings.title" v-bind="settings" :visible.sync="settings.visible" :wrapper-closable="false" class="detail-drawer" @open="dialogOpen">
    <div>
      <div class="close-drawer" @click="$refs.drawer.closeDrawer()">
        <i class="el-icon-arrow-right" />
      </div>
      <div class="demo-drawer-body">
        <el-form ref="form" v-bind="form" :rules="rules">
          <p class="stitle">分销商信息</p>
          <el-form-item prop="value1" label="分销商名称:">
            <el-input v-model="form.model.value1" placeholder="请输入" maxlength="30" />
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="demo-drawer__footer">
      <el-button type="primary" plain size="mini" @click="handleCancel()">取 消</el-button>
      <el-button type="primary" size="mini" :loading="loading" @click="handleSave()">{{ loading ? '提交中 ...' : '确 定' }}</el-button>
    </div>
  </el-drawer>
</template>

<script>
import { internetApi } from '@/api/api'

const defaultModel = {
  value1: '',
  value015: ''
}
export default {
  components: {},
  data() {
    return {
      loading: false,
      settings: {
        title: '',
        visible: false,
        size: '1000px',
        appendToBody: true,
        showClose: false
      },
      type: '',
      gameBitGroupId: '',
      form: {
        labelWidth: '130px',
        labelPosition: 'right',
        model: Object.assign({}, defaultModel)
      },
      row: {},
      rules: {
        value1: [{ required: true, message: '分销商名称不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 初始化函数：打开赋值在此操作
    async show(row) {
      this.form.model = Object.assign({}, defaultModel)
      this.settings.visible = true
      if (row) {
        this.type = 'edit'
        this.settings.title = '编辑分销商'
        const { data } = await this.$request(internetApi.gameBitGroupDetail)
        this.form.model = data
      } else {
        this.type = 'add'
        this.settings.title = '新建分销商'
      }
    },
    // 保存
    handleSave() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          this.loading = true
          if (this.type === 'add') {
            this.$request(internetApi.gameBitGroupAdd, this.form.model).then((res) => {
              if (res.code === 1) {
                this.$message({
                  message: '保存成功',
                  type: 'success'
                })
                this.$emit('refresh')
                this.settings.visible = false
              }
            })
          } else {
            this.$request(internetApi.gameBitGroupEdit, this.form.model).then((res) => {
              if (res.code === 1) {
                this.$message({
                  message: '保存成功',
                  type: 'success'
                })
                this.$emit('refresh')
                this.settings.visible = false
              }
            })
          }
          this.loading = false
        }
      })
    },
    // 取消
    handleCancel() {
      this.form.model = Object.assign({}, defaultModel)
      this.settings.visible = false
    },
    //弹窗打开时，清除所有校验
    dialogOpen() {
      //dialog是懒加载，所以要用$nextTick
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-drawer__wrapper {
  ::v-deep.el-drawer {
    overflow: inherit;
  }
}
::v-deep.demo-drawer-body {
  padding: 20px;
  height: calc(100vh - 180px) !important;
  overflow: auto;
  .el-select,
  .el-cascader,
  .el-input {
    width: 318px;
    height: 36px !important;
    background: #ffffff;
    border-radius: 5px;
  }
  ::v-deep.el-form-item__label {
    font-weight: 400;
    color: #666666;
  }
  .textareabg {
    width: 800px;
  }
  .el-form-item {
    width: 50%;
    display: inline-block;
  }
  p.stitle {
    font-size: 16px;
    font-family: Source Han Sans SC;
    font-weight: 400;
    color: #333333;
    margin-top: 0px;
    line-height: 50px;
  }
}
.demo-drawer__footer {
  position: absolute;
  text-align: right;
  bottom: 0px;
  width: 100%;
  height: 97px;
  left: 0;
  background: #ffffff;
  padding: 35px 20px;
  box-shadow: 0px -32px 46px 0px rgba(126, 126, 126, 0.09);
}
</style>
